/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.main;

import java.util.List;

import mrusanov.towers.main.sounds.Event;
import mrusanov.towers.players.Player;
import mrusanov.towers.players.Tower;
import mrusanov.towers.resources.PlayerIncome;
import mrusanov.towers.resources.Resource;

import com.google.common.collect.Lists;

public class TurnStats {
	private Tower playerTowerBefore;
	private Tower playerTowerAfter;
	private Tower enemyTowerBefore;
	private Tower enemyTowerAfter;
	private PlayerIncome playerIncomeBefore;
	private PlayerIncome playerIncomeAfter;
	private PlayerIncome enemyIncomeBefore;
	private PlayerIncome enemyIncomeAfter;

	private final Player player;
	private final Player enemy;

	public TurnStats(Player player, Player enemy) {
		this.player = player;
		this.enemy = enemy;
	}

	public void rememberBeforeState() {
		playerIncomeBefore = player.getIncome().clone();
		enemyIncomeBefore = enemy.getIncome().clone();
		playerTowerBefore = player.getTower().clone();
		enemyTowerBefore = enemy.getTower().clone();
	}

	public void rememberAfterState() {
		playerIncomeAfter = player.getIncome().clone();
		enemyIncomeAfter = enemy.getIncome().clone();
		playerTowerAfter = player.getTower().clone();
		enemyTowerAfter = enemy.getTower().clone();
	}

	public List<Event> calculateEvents() {
		List<Event> events = Lists.newArrayList();

		if (!enemyTowerAfter.isAlive() || !playerTowerAfter.isAlive()) {
			events.add(Event.DESTROY_TOWER);
		}

		if (enemyTowerAfter.getTowerHeight() < enemyTowerBefore.getTowerHeight()
				|| playerTowerAfter.getTowerHeight() < playerTowerBefore.getTowerHeight()) {
			events.add(Event.DAMAGE_TOWER);
		}

		if (enemyTowerAfter.getWallHeight() < enemyTowerBefore.getWallHeight()
				|| playerTowerAfter.getWallHeight() < playerTowerBefore.getWallHeight()) {
			events.add(Event.DAMAGE_WALL);
		}

		if (enemyTowerAfter.getTowerHeight() > enemyTowerBefore.getTowerHeight()
				|| playerTowerAfter.getTowerHeight() > playerTowerBefore.getTowerHeight()) {
			events.add(Event.BUILD_TOWER);
		}

		if (enemyTowerAfter.getWallHeight() > enemyTowerBefore.getWallHeight()
				|| playerTowerAfter.getWallHeight() > playerTowerBefore.getWallHeight()) {
			events.add(Event.BUILD_WALL);
		}

		int playersIncomeChanged = incomeChanged(playerIncomeBefore, playerIncomeAfter);
		int enemysIncomeChanged = incomeChanged(enemyIncomeBefore, enemyIncomeAfter);

		if (playersIncomeChanged > 0 || enemysIncomeChanged > 0) {
			events.add(Event.INCREASE_INCOME);
		}

		if (playersIncomeChanged < 0 || enemysIncomeChanged < 0) {
			events.add(Event.DECREASE_INCOME);
		}

		if (events.isEmpty()) {
			events.add(Event.TURN_END);
		}

		return events;
	}

	/**
	 * 
	 * @param before
	 * @param after
	 * @return +1 in case of income increase, 0 if nothing changed, -1 if income
	 *         decreased
	 */
	private int incomeChanged(PlayerIncome before, PlayerIncome after) {
		for (Resource resource : Resource.values()) {
			int resourceBefore = before.getResource(resource);
			int resourceAfter = after.getResource(resource);

			if (resourceAfter > resourceBefore) {
				return 1;
			}

			if (resourceAfter < resourceBefore) {
				return -1;
			}
		}

		return 0;
	}
}
